/**
 * Copyright 2010 Kurtis Nusbaum
 *
 * This file is part of RemoteRunner.  
 *
 * RemoteRunner is free software: you can 
 * redistribute it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation, either version 2 of the 
 * License, or (at your option) any later version.  
 *
 * RemoteRunner is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.  You should have received a copy of the GNU  General 
 * Public License along with RemoteRunner. If not, see 
 * http://www.gnu.org/licenses/.
 */

package org.klnusbaum.remoterunner;

import android.util.Log;
import android.os.Bundle;
import android.view.View;
import android.app.AlertDialog;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface;
import android.content.Context;
import android.widget.EditText;
import android.text.TextWatcher;
import android.text.Editable;

/**
 * Class for editing commands.
 */
public class EditCommandDialog extends AlertDialog implements TextWatcher{

  /** EditText with actual command in it. */
  private EditText commandEdit;
  /** EditText with description of command. */
	private EditText descriptionEdit;
  /** Tag for loggin */
	private static final String TAG = "EDITCOMMAND";

	static final int BUTTON_OK = DialogInterface.BUTTON_POSITIVE;

	/** Database in which the command is/will be stored. */
	private HostsDB hostsDatabase;

	/** If we're editing a command, this is it's ID. Otherwise
		is set to HostsDB.INVALID_COMMAND_ID */
	private int commandID;

	/** Listner to be used for the Ok and Cancel buttons */
	private OnClickListener buttonListener;

	private static final String EXTRA_MY_STATE = "MY_STATE";
	private static final String EXTRA_COMMAND = "COMMAND";
	private static final String EXTRA_COMMAND_DESC = "COMMAND_DESC";


	/**
   * Constructs an EditCommandDialog. Use this constructor when editing
	 * an existing command.
   *
	 * @param context The context in which the dialog is being created.
	 * @param buttonListener Listener to be used for the Ok and Cancel buttons.
	 * @param commandID The ID of the command that is being edited. 
		 @param hostsDatabase The database containing all host information.
	 */
	public EditCommandDialog(Context context, OnClickListener buttonListener,
		DialogInterface.OnDismissListener onDismissListener,
		int commandID, HostsDB hostsDatabase)
	{
		super(context);
		this.buttonListener = buttonListener;
		init(commandID, onDismissListener, hostsDatabase);
	}

	/**
   * Constructs an EditCommandDialog. Use this constructor when adding
	 * a new command.
   *
	 * @param context The context in which the dialog is being created.
	 * @param buttonListener Listener to be used for the Ok and Cancel buttons.
		 @param hostsDatabase The database containing all host information.
	 */
	public EditCommandDialog(Context context, OnClickListener buttonListener,
		DialogInterface.OnDismissListener onDismissListener,
		HostsDB hostsDatabase)
	{
		super(context);
		this.buttonListener = buttonListener;
		init(HostsDB.INVALID_COMMAND_ID, onDismissListener, hostsDatabase);
	}

	/**
   * Initializes the dialog with the given attributes.
	 *
	 * @param commandID The ID of the command being edited. 
	 * If no command is being edited
	 * and instead a new one is being added, this value should be set to
	 * HostsDB.INVALID_COMMAND_ID.
		 @param hostsDatabase The database containing all host information.
	 */
	private void init(int commandID, 
		DialogInterface.OnDismissListener onDismissListener,
		HostsDB hostsDatabase)
	{
		this.commandID = commandID;
		this.hostsDatabase = hostsDatabase;
		setOnDismissListener(onDismissListener);
	}

	@Override
  public void onCreate(Bundle savedInstanceState){
		View view = getLayoutInflater().inflate(R.layout.edit_command_layout, null);
    setView(view);

    commandEdit = (EditText) view.findViewById(R.id.commandEdit);
    descriptionEdit = (EditText) view.findViewById(R.id.descriptionEdit);


		if(isEditing()){
			commandEdit.setText(
				hostsDatabase.getCommand(commandID));
			descriptionEdit.setText(
				hostsDatabase.getCommandDescription(commandID));
    	setTitle(R.string.edit_command);
		}
		else{
    	setTitle(R.string.add_command);
		}

		commandEdit.addTextChangedListener(this);	

		setButton(
			BUTTON_OK, 
			getContext().getString(android.R.string.ok),
			buttonListener);
		setButton(
			BUTTON_NEGATIVE, 
			getContext().getString(android.R.string.cancel), 
			buttonListener);
    super.onCreate(savedInstanceState);
		validate();
  }

	@Override
	public Bundle onSaveInstanceState(){
		Bundle toReturn = super.onSaveInstanceState();
		Bundle state = new Bundle();
		state.putCharSequence(EXTRA_COMMAND, commandEdit.getText());
		state.putCharSequence(EXTRA_COMMAND_DESC, descriptionEdit.getText());
		toReturn.putBundle(EXTRA_MY_STATE, state);
		return toReturn;
	}	

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState){
		super.onRestoreInstanceState(savedInstanceState);
		Bundle state = savedInstanceState.getBundle(EXTRA_MY_STATE);
		commandEdit.setText(state.getCharSequence(EXTRA_COMMAND));
		descriptionEdit.setText(state.getCharSequence(EXTRA_COMMAND_DESC));
		validate();
	}

	

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count){
	
	}

	@Override
	public void beforeTextChanged(
		CharSequence s, int start, int count, int after)
	{
	
	}

	@Override
	public void afterTextChanged(Editable editable){
		validate();
	}


	/** 
	 * Validates whether or not the current inputs the user has specied 
   * are valid. If they aren't the Ok button is disabled.
   */
	private void validate(){
		if(!commandEdit.getText().toString().equals("")){
			getButton(BUTTON_OK).setEnabled(true);
		}
		else{
			getButton(BUTTON_OK).setEnabled(false);
		}
	}	
	
	/** 
	 * Gets what the user has entered in for the command.
   */
	public String getCommand(){
		return commandEdit.getText().toString();
	}	

	/**
	 * Gets what the user has entered in for the description
	 */
	public String getCommandDesc(){
		return descriptionEdit.getText().toString();
	}

	/**
	 * Gets the ID of the command currently being edited.
	 *
	 * @return The ID of the command currently being edited. If
	 * no command is being edited (just added) the this will return
	 * HostsDB.INVALID_COMMAND_ID.
	 */
	public int getCommandID(){
		return commandID;
	}

	/**
	 * Determines whether or not we're editing an existing command
	 * or just adding a new one.
	 * 
	 * @return Whether or not we're editing an existing command.
	 */
	public boolean isEditing(){
		return commandID != HostsDB.INVALID_COMMAND_ID;
	}

		
}
